%perpare testing data
% x = randn(20, 100);
% y = zeros(20, 100);
% y(x>0)     = 1;
% dataMatrix = y;
% index = 1:100;

y              = zeros(300, 1000);
y(10:100, 1:200)        = 1;
y(50:250, 900:1000)    = 1;
y(17:150, 200:300)      = 1;
y(1:200, 100:200)      = 1;
dataMatrix     = y;



% summerize the missing data
% missSeries = kron(ones(1, size(y, 1)), index);
% missindex  = logical(reshape(y', 1, numel(y)));
% missSeries = missSeries(missindex);
% TABLE      = tabulate(missSeries);
% TABLE(:,3) =   TABLE(:,2)./size(y,2);
% 
% % test fitness function
% startPoint    = 1;
% endPoint      = 5;
% rIndex        = false(20,1);
% rIndex(1:2:5) = true;
alpha         = 4;
beta          = 1;
area          = numel(y);
% 
% goal          = fillMatrix( startPoint, endPoint, rIndex,  dataMatrix, alpha, beta, area );

% genetic algorithm

          


sparseIndex = sum(dataMatrix,2)/size(dataMatrix,2);
dataMatrix(sparseIndex>=0.8)   = 0;


count = 1;
tic
while true
    dataMatrix                  = dataMatrix(~sum(dataMatrix,2) == 0, :);
    [dataMatrix, reduceIndex]   = cutMargin( dataMatrix );
    nvars = size(dataMatrix,1)+ 2;
    A             =  [1, -1, zeros(1, size(dataMatrix,1))] ;
    b             =  0;
    Aeq           =  [];
    beq           =  [];
    LB            =  [1;1;zeros(size(dataMatrix,1), 1)];
    UB            =  [size(dataMatrix,2);size(dataMatrix,2);ones(size(dataMatrix,1), 1)];
    nonlcon       =  [];
    IntCon        =   1:nvars;
    options = gaoptimset('UseParallel', 'always', 'Vectorized', 'off');
    fitnessfcn    = @(x) fillMatrix(x(1), x(2), x(3:(size(dataMatrix,1)+2)), dataMatrix, alpha, beta);
    [x,fval,exitflag,output,population,scores]  ...
                                  = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,IntCon,options);
    subMatix                      = dataMatrix(logical(x(3:end)), x(1):x(2));
    disp(num2str(sum(subMatix(:))/numel(subMatix)));
    dataMatrix(logical(x(3:end)), x(1):x(2)) = 0 ;
    
    disp(['loop',num2str(count),' time']);
    disp(num2str(sum(dataMatrix(:))));
    count = count + 1;
    if  sum(dataMatrix(:)) == 0
        disp(num2str(sum(dataMatrix(:))));
        break
    end
end
toc












